

package cn.shengchao.examstar.distribute.infrastrure.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.shengchao.base.enums.ProductType;
import cn.shengchao.examstar.distribute.infrastrure.mapper.DistributeOrderTaskMapper;
import cn.shengchao.examstar.distribute.infrastrure.mapper.DistributeOrderTaskSaleMapper;
import cn.shengchao.examstar.distribute.infrastrure.po.DistributeOrderTask;
import cn.shengchao.examstar.distribute.infrastrure.po.DistributeOrderTaskSale;
import cn.shengchao.examstar.distribute.infrastrure.query.DistributeAmountQuery;
import cn.shengchao.examstar.distribute.infrastrure.query.DistributeUserSaleDetailQuery;
import cn.shengchao.examstar.distribute.infrastrure.query.SaleAmountQuery;
import cn.shengchao.examstar.distribute.infrastrure.service.DistributeOrderTaskSaleService;
import cn.shengchao.examstar.distribute.infrastrure.vo.DistributeOrder;
import cn.shengchao.examstar.distribute.infrastrure.vo.DistributeSale;
import cn.shengchao.examstar.distribute.infrastrure.vo.DistributeSaleUserGroup;
import cn.shengchao.examstar.distribute.infrastrure.vo.SaleMember;
import cn.shengchao.examstar.rpc.uddsys.req.PlatformDistributeUserStatisticQuery;
import cn.shengchao.examstar.rpc.uddsys.resp.DistributeUserMonthStaticVo;
import cn.shengchao.examstar.rpc.uddsys.resp.DistributeUserProfitDetailVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DistributeOrderTaskSaleServiceImpl extends ServiceImpl<DistributeOrderTaskSaleMapper, DistributeOrderTaskSale> implements DistributeOrderTaskSaleService {

    @Autowired
    private DistributeOrderTaskSaleMapper distributeOrderTaskSaleMapper;

    @Autowired
    private DistributeOrderTaskMapper distributeOrderTaskMapper;

    @Override
    public List<DistributeUserProfitDetailVo> getDistributeUserProfitDetailList(DistributeUserSaleDetailQuery distributeUserSaleDetailQuery) {
        return baseMapper.getDistributeUserProfitDetailList(distributeUserSaleDetailQuery);
    }

    /**
     * 获取一堆用户销售金额
     */
    @Override
    public List<DistributeSaleUserGroup> getSaleAmountGroupForUsers(SaleAmountQuery saleAmountQuery) {
        List<DistributeSaleUserGroup> distributeSales = distributeOrderTaskSaleMapper.getDistributeUserGroupSaleAmount(saleAmountQuery);
        if (CollUtil.isNotEmpty(distributeSales)) {
            for (DistributeSaleUserGroup distributeSale : distributeSales) {
                if (distributeSale.getCourseSum() == null) {
                    distributeSale.setCourseSum(0L);
                }
                if (distributeSale.getMemberSum() == null) {
                    distributeSale.setMemberSum(0L);
                }
            }
        }
        return distributeSales;
    }

    /**
     * 获取销售金额
     */
    @Override
    public DistributeSale getSaleAmount(SaleAmountQuery saleAmountQuery) {
        DistributeSale distributeSale = distributeOrderTaskSaleMapper.getSaleAmount(saleAmountQuery);
        if (distributeSale == null) {
            distributeSale = new DistributeSale();
        }
        if (distributeSale.getCourseSum() == null) {
            distributeSale.setCourseSum(0L);
        }
        if (distributeSale.getMemberSum() == null) {
            distributeSale.setMemberSum(0L);
        }
        if (distributeSale.getOrderAmount() == null) {
            distributeSale.setOrderAmount(BigDecimal.ZERO);
        }

        return distributeSale;
    }

    @Override
    public Map<Long, Long> saleMemberSum(List<Long> saleIds, Date startTime, Date endTime) {
        MPJLambdaWrapper<DistributeOrderTask> wrapper = new MPJLambdaWrapper<DistributeOrderTask>()
                .select("t1.sale_user_id as saleUserId", "count(distinct t.order_id) as orderSum")
                .leftJoin(DistributeOrderTaskSale.class, DistributeOrderTaskSale::getDistributeTaskId, DistributeOrderTask::getId)
                .ne(DistributeOrderTask::getDataStatus, 9)
                .eq(DistributeOrderTask::getProductType, ProductType.PRODUCT_MEMBER.name())
                .ge(startTime != null, DistributeOrderTask::getOrderTime, startTime)
                .le(endTime != null, DistributeOrderTask::getOrderTime, endTime)
                .in(DistributeOrderTaskSale::getSaleUserId, saleIds)
                .groupBy(DistributeOrderTaskSale::getSaleUserId);
        List<SaleMember> distributeOrderTasks = distributeOrderTaskMapper.selectJoinList(SaleMember.class, wrapper);
        if (distributeOrderTasks.isEmpty()) {
            return Map.of();
        }
        return distributeOrderTasks.stream().collect(Collectors.toMap(SaleMember::getSaleUserId, SaleMember::getOrderSum));
    }

    @Override
    public List<DistributeOrder> getSaleOrder(SaleAmountQuery saleAmountQuery) {
        MPJLambdaWrapper<DistributeOrderTask> wrapper = new MPJLambdaWrapper<DistributeOrderTask>()
                .select("t.order_id orderId,t1.distribute_amount,t.pay_user_id payUserId,t.data_status distributeOrderStatus")
                .leftJoin(DistributeOrderTaskSale.class, DistributeOrderTaskSale::getDistributeTaskId, DistributeOrderTask::getId)
                .ne(DistributeOrderTask::getDataStatus, 9)
                .in(saleAmountQuery.getStatus() != null && !saleAmountQuery.getStatus().isEmpty(), DistributeOrderTask::getDataStatus, saleAmountQuery.getStatus())
                .eq(saleAmountQuery.getSaleUserId() != null, DistributeOrderTaskSale::getSaleUserId, saleAmountQuery.getSaleUserId())
                .eq(saleAmountQuery.getPayUserId() != null, DistributeOrderTask::getPayUserId, saleAmountQuery.getPayUserId())
                .eq(DistributeOrderTaskSale::getDistributeUserId, saleAmountQuery.getDistributeUserId())
                .ge(saleAmountQuery
                        .getStartDistributeTime() != null, DistributeOrderTaskSale::getDistributeTime, saleAmountQuery
                        .getStartDistributeTime())
                .le(saleAmountQuery
                        .getEndDistributeTime() != null, DistributeOrderTaskSale::getDistributeTime, saleAmountQuery
                        .getEndDistributeTime())
                .ge(saleAmountQuery
                        .getStartSaleTime() != null, DistributeOrderTask::getOrderTime, saleAmountQuery
                        .getStartSaleTime())
                .le(saleAmountQuery
                        .getEndSaleTime() != null, DistributeOrderTask::getOrderTime, saleAmountQuery
                        .getEndSaleTime()
                )
                .orderByDesc(DistributeOrderTask::getOrderTime);

        return distributeOrderTaskMapper.selectJoinList(DistributeOrder.class, wrapper);
    }

    @Override
    public List<DistributeUserMonthStaticVo> listStaticListSortForUpLevelUser(PlatformDistributeUserStatisticQuery saleAmountQuery) {
        return List.of();
    }

    @Override
    public List<DistributeUserProfitDetailVo> getDistributeEmployeeContributeDetailList(DistributeUserSaleDetailQuery distributeUserSaleDetailQuery) {
        return baseMapper.getDistributeEmployeeContributeDetailList(distributeUserSaleDetailQuery);
    }


    @Override
    public DistributeSaleUserGroup getSaleAmountForDistributeUser(DistributeAmountQuery saleAmountQuery) {
        return baseMapper.getSaleAmountForDistributeUser(saleAmountQuery);
    }
}
